package com.nbilyk.ntween {

	public class NTweenModifier {
		
		/**
		 * The defaultInitTweenMethod must be a method with the signature 
		 * func(tween:NTween):void.
		 * 
		 * This method will be 
		 * @default NTweenModifier.overwrite
		 */
		public static var defaultInitTweenMethod:Function = overwrite;
		
		
		/**
		 * The overwrite method will take a given tween and find existing tweens with the same properties, 
		 * updating the old tween with the new values, and removing those properties from the new tween.
		 */
		public static function overwrite(tween:NTween):void {
			for each (var obj:ITimeUpdatable in NTimeEngine.instance.registeredObjects) {
				if (obj is NTween) {
					var iTween:NTween = NTween(obj);
					if (iTween.target == tween.target) {
						for (var all:String in tween.props) {
							if (iTween.props.hasOwnProperty(all)) {
								iTween.props[all] = tween.props[all];
								delete tween.props[all];
								delete tween.initialValues[all];
							}
						}
					}
				}
			}
		}
		
		/**
		 * Finds all tweens with the given target and removes the properties in [props]
		 * This will prevent two tweens from competing with each other.  
		 * This method is different from the overwrite mode which keeps the old tween but updates 
		 * the old tween's properties with the new values. 
		 */
		public static function replace(tween:NTween):void {
			for each (var obj:ITimeUpdatable in NTimeEngine.instance.registeredObjects) {
				if (obj is NTween) {
					var iTween:NTween = NTween(obj);
					if (iTween.target == tween.target) {
						for (var all:String in tween.props) {
							if (iTween.props.hasOwnProperty(all)) {
								delete iTween.props[all];
								delete iTween.initialValues[all];
							}
						}
					}
				}
			}
		}
	}
}